package com.mygame.Svetofor.game;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.mygame.Svetofor.ThemeManager;

import java.util.HashMap;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * User: Andrey
 * Date: 16.03.13
 * Time: 14:15
 * To change this template use File | Settings | File Templates.
 */
public class ImageAdaptor extends BaseAdapter
    {
        private Context context;

        private HashMap<Integer,ImageView> map = new HashMap<Integer, ImageView>();
        public HashMap<Integer,Boolean> selectedMap = new HashMap<Integer, Boolean>();

        public HashMap<Integer, ImageView> getMap() {
            return map;
        }

        public void selectItem(int i, int j) {
            selectItem(j*5 + i);
        }

        public void unSelectAll() {
            for(int i = 0; i < 25; i++) {
                selectedMap.put(i,false);
            }
        }

        public int[][] getSelectedItems() {

            int[][] res = new int[5][5];
            for(int i =0; i < 5; i++)
                for(int j = 0; j < 5; j++)
                    if(selectedMap.get(j*5 + i)) {
                        res[i][j] = 1;
                    } else {
                        res[i][j] = 0;
                    }
            return res;
        }

        public void selectItem(int position) {
            selectedMap.put(position,true);
        }

        private int[][] m;



        public ImageAdaptor(Context c,int[][] m) {
            context = c;
            this.m = m;
            ImageView imageView;
            for (int j = 0; j < 5; j++)
               for(int i = 0; i < 5; i++) {
                   imageView = new ImageView(context);
                   imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
                   imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                   imageView.setImageResource(ThemeManager.INSTANCE.getImage(getImageId(5 * j + i) - 1));

                   imageView.setOnTouchListener(new View.OnTouchListener() {
                       public boolean onTouch(View v, MotionEvent event) {
                           v.setSelected(true);
                           addSelect((ImageView) v);
                           return true;
                       }
                   });
                   map.put(5*j + i, imageView);
                   selectedMap.put(5*j + i, false);
               }
        }

        private int getImageId(int position){
            return m[position%5][position/5];
        }

        //---returns the number of images---
        public int getCount() {
            return map.size();
        }

        private void addSelect(ImageView iv) {
            Iterator<Integer> it = map.keySet().iterator();
            Integer i;
            while(it.hasNext()) {
                i = it.next();
                if (map.get(i).equals(iv)) {
                    selectItem(i);
                }
            }
        }

        //---returns the ID of an item---
        public Object getItem(int position) {
            return map.get(position);
        }

        public long getItemId(int position) {
            return getImageId(position);
        }

        //---returns an ImageView view---
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView = (ImageView) getItem(position);
            return imageView;
        }

    }